package com.point.community;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.demo.SysRoleInterface;
import com.alibaba.dubbo.demo.SysUserRoleInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysRole;
import com.alibaba.dubbo.demo.bean.SysUserRole;
import com.point.community.bean.dwzPageData;

@SuppressWarnings("serial")
public class SysRoleAction extends JsonAction {
	
	Logger logger = Logger.getLogger(SysRoleAction.class);
	@Autowired
	private SysRoleInterface sysRoleService;
	@Autowired
	private SysUserRoleInterface sysUserRoleService;
	
	private String msg = "";
	private boolean success = false;
	private String key = "";
	private String obj = "";
	private PageBean pageBean;
	private String statusCode,message,navTabId,rel,callbackType,forwardUrl,confirmMsg;
	
	private List<SysRole> sysRoleList = new ArrayList<SysRole>();
	private SysRole sysRole = new SysRole();

	public SysRoleInterface getSysRoleService() {
		return sysRoleService;
	}

	public void setSysRoleService(SysRoleInterface sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	public SysUserRoleInterface getSysUserRoleService() {
		return sysUserRoleService;
	}

	public void setSysUserRoleService(SysUserRoleInterface sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}

	public List<SysRole> getSysRoleList() {
		return sysRoleList;
	}

	public void setSysRoleList(List<SysRole> sysRoleList) {
		this.sysRoleList = sysRoleList;
	}
	
	public SysRole getSysRole() {
		return sysRole;
	}

	public void setSysRole(SysRole sysRole) {
		this.sysRole = sysRole;
	}

	@SuppressWarnings("unchecked")
	public String webQueryAllSysRole()
	{
		logger.error("enter webQueryAllSysRole");
		
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
	    if(StringUtils.isEmpty(numPerPage)){
	    	numPerPage = "20";
	    }
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
		
		String start = getParameterString("start");
		if (start != null && !start.equals("")) {
			pageBean = new PageBean(start, PAGE_MAX_RECORD_COUNT);
		} else {
			pageBean = new PageBean("0", PAGE_MAX_RECORD_COUNT);
		}
		//参数
		String rolecode = "", rolename = "";
		if(request.getParameterValues("rolecode")!=null)
			rolecode = getParameterString("rolecode");
		if(request.getParameterValues("rolename")!=null)
			rolename = getParameterString("rolename");		 
		
		pageBean = sysRoleService.queryAllSysRole(pageBean, rolecode, rolename);
		
		sysRoleList = pageBean.getResultList();

		request.getSession().setAttribute("sysRoleList", sysRoleList);
		request.getSession().setAttribute("rolecode", rolecode);
		request.getSession().setAttribute("rolename", rolename);
		request.getSession().setAttribute("numPerPage", nNumPerPage);

		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = Integer.valueOf(nCurrentPage).toString();
		dwz.numPerPage =  Integer.valueOf(nNumPerPage).toString();
		dwz.totalCount = (long) pageBean.getTotal();
		request.getSession().setAttribute("sysRoleDwzData", dwz);
		
		return "sysRole";
	}
	
	public String jsonQueryAllSysRole()
	{
		logger.error("jsonQueryAllSysRole");
		//参数
		String rolecode = "", rolename = "";
		if (request.getParameterValues("rolecode") != null)
			rolecode = getParameterString("rolecode");
		if (request.getParameterValues("rolename") != null)
			rolename = getParameterString("rolename");

		sysRoleList = sysRoleService.queryAllSysRoleList(rolecode, rolename);
		boolean ret = sysRoleList==null?false:sysRoleList.size()>0;
		if (ret) 
		{
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
	
	public boolean userHasRole(int roleid,List<SysUserRole> rolelist)
	{
		if(rolelist==null)
			return false;
		for(int i=0;i<rolelist.size();i++)
		{
			if(rolelist.get(i).getRoleid()==roleid)
				return true;
		}
		return false;
	}
	
	public String jsonQueryRightSysRole()
	{
		logger.error("jsonQueryAllSysRole");
		//参数
		String userid = "";
		//String usercode = "";
		if (request.getParameterValues("userid") != null)
			userid = getParameterString("userid");
		//if (request.getParameterValues("usercode") != null)
		//	usercode = getParameterString("usercode");

		List<SysRole> roleList = sysRoleService.queryAllSysRoleList("", "");
		List<SysUserRole> rightlist = sysUserRoleService.queryAllSysUserRoleList(Integer.parseInt(userid), "", "", 0, "", "");
		boolean ret = roleList==null?false:roleList.size()>0; 
		if (ret) 
		{
			for(int i=0;i<roleList.size();i++)
			{
				SysRole  item = roleList.get(i);
				if(item.getIsvalid()==0)
					continue;
				if(userHasRole(item.getRoleid(),rightlist))
					item.setIsvalid(1);
				else
					item.setIsvalid(0);
				sysRoleList.add(item);				
			}
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
	public String queryOneSysRole() {
		int roleid = -1;
		if (request.getParameterValues("roleid") != null)
			roleid = getParameterInteger("roleid");
		sysRole = sysRoleService.queryOneSysRole(roleid);
		request.getSession().setAttribute("sysRole", sysRole);
		return "oneSysRole";
	}

	/**
	 * 保存用户信息
	 */
	public String webSaveSysRole() {
		
		String deptrightid = getStrParam("orgLookup.deptrightid");
		if(StringUtils.isEmpty(deptrightid))
			deptrightid = getStrParam("deptrightid");
		String deptright = getStrParam("orgLookup.deptright");
		if(StringUtils.isEmpty(deptright))
			deptright = getStrParam("deptright");
		
		String rolecode = getStrParam("rolecode");
		if(sysRoleService.queryOneSysRole(rolecode)==null)
		{
			logger.error("webSaveSysRole sysRoleService.queryOneSysRole(rolecode)!=null");
			sysRole.setRolecode(rolecode);
			sysRole.setRolename(getStrParam("rolename"));
			sysRole.setRemark(getStrParam("remark"));
			sysRole.setDataright(1);
			sysRole.setDeptrightid(deptrightid);
			sysRole.setDeptright(deptright);
			sysRole.setIsvalid(1);
			boolean ret =  sysRoleService.saveSysRole(sysRole);
			if(ret)
			{
				success = true;
				logger.info("保存角色信息成功!");
				statusCode="200";
				message="操作成功";
			}
			else
			{
				success = false;
				logger.info("保存角色信息异常!");
				statusCode="300";
				message="操作失败";
			}
		}
		else
		{
			logger.error("webSaveSysRole sysRoleService.queryOneSysRole(rolecode)==null");
			success = false;
			logger.info("角色编号重复!");
			statusCode="300";
			message="操作失败：角色编号重复";
		}
		navTabId="roleinfo";
		rel="";
		callbackType="closeCurrent";
		forwardUrl="";
		confirmMsg="";
		return SUCCESS;
	}
	/**
	 * 保存用户信息
	 */           
	public String webUpdateSysRole() {
		int roleid = getIntParam("roleid");
		sysRole = sysRoleService.queryOneSysRole(roleid);
		boolean ret =  false;
		
		String deptrightid = getStrParam("orgLookup.deptrightid");
		if(StringUtils.isEmpty(deptrightid))
			deptrightid = getStrParam("deptrightid");
		String deptright = getStrParam("orgLookup.deptright");
		if(StringUtils.isEmpty(deptright))
			deptright = getStrParam("deptright");
		
		if(sysRole!=null)
		{
			sysRole.setRolecode(getStrParam("rolecode"));
			logger.error("getStrParam(rolecode)=="+getStrParam("rolecode"));
			sysRole.setRolename(getStrParam("rolename"));
			sysRole.setRemark(getStrParam("remark"));
			sysRole.setDataright(1);
			sysRole.setDeptrightid(deptrightid);
			sysRole.setDeptright(deptright);
			sysRole.setIsvalid(getIntParam("isvalid"));
			ret =  sysRoleService.updateSysRole(sysRole);
		}
	   if (ret) 
	   {
			success = true;
			logger.info("更新角色信息成功!");
			
			statusCode="200";
			message="操作成功";
			navTabId="roleinfo";
			rel="";
			callbackType="closeCurrent";
			forwardUrl="";
			confirmMsg="";
		}
	   else
	   {
		    success = true;
			logger.info("更新角色信息异常!");
			
			statusCode="200";
			message="操作失败";
			navTabId="userinfo";
			rel="";
			callbackType="roleinfo";
			forwardUrl="";
			confirmMsg="";
	   }
		return SUCCESS;
	}	
	// 更新部门状态标志
	public String updateSysRoleFlag() {
		int roleid = -1;
		if (request.getParameterValues("roleid") != null)
			roleid = getParameterInteger("roleid");
		int valid = 1;
		if (request.getParameterValues("valid") != null)
			valid = getParameterInteger("valid");
		boolean ret = false;
		sysRole = sysRoleService.queryOneSysRole(roleid);
		if (sysRole != null) {
			sysRole.setIsvalid(valid);
			ret = sysRoleService.updateSysRoleFlag(roleid, valid);
		}

		if (ret) {
			success = true;
			logger.info("更新角色信息成功!");

			statusCode = "200";
			message = "操作成功";
			navTabId = "roleinfo";
			rel = "";
			callbackType = "";
			forwardUrl = "";
			confirmMsg = "";
		} else {
			success = true;
			logger.info("更新角色信息异常!");

			statusCode = "200";
			message = "操作失败";
			navTabId = "roleinfo";
			rel = "";
			callbackType = "";
			forwardUrl = "";
			confirmMsg = "";
		}
		return SUCCESS;
	}
}
